package com.springboot.demo.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ContextWareUtil implements ApplicationContextAware {
    private static ApplicationContext APPLICATIONCONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ContextWareUtil.APPLICATIONCONTEXT = applicationContext;
    }

    public static Object getBean(String name) {
        return ContextWareUtil.APPLICATIONCONTEXT.getBean(name);
    }

    public static <T> Object getBean(Class<T> className) {
        return ContextWareUtil.APPLICATIONCONTEXT.getBean(className);
    }
}

